"use strict";
module.exports = {
  /**
   * 新增分类
   * @url client/classification/kh/addClassification 前端调用的url参数地址
   * data 请求参数
   * @param {String} params1  参数1
   */
  main: async (event) => {
    let { data = {}, userInfo, util, filterResponse, originalParam } = event;
    let { customUtil, uniID, config, pubFun, vk, db, _, $ } = util;
    const { name, type, uid } = data;
    // 业务逻辑开始-----------------------------------------------------------
    if (!name)
      return {
        code: -1,
        msg: "请输入分类名称",
      };
    if (!type)
      return {
        code: -1,
        msg: "缺少分类所属应用",
      };
    let res = await vk.baseDao.selects({
      dbName: "dfb-classification",
      getCount: true,
      getMain: true,
      pageSize: -1,
      whereJson: {
        user_id: uid,
        type: type,
      },
      fieldJson: {
        user_id: false,
      },
    });
    if (res.find((item) => item.name == name)) {
      return {
        code: -1,
        msg: "分类名称重复",
      };
    }
    let id = await vk.baseDao.add({
      dbName: "dfb-classification", // 表名
      dataJson: {
        name,
        type,
        user_id: uid,
      },
    });
    if (!id)
      return {
        code: -1,
        msg: "新增失败",
      };
    // 业务逻辑结束-----------------------------------------------------------
    return {
      code: 0,
      msg: "新增成功",
    };
  },
};
